<template>
  <div class="departments">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <!-- 页面title -->
        <!-- <div slot="header">
          <span>组织架构</span>
        </div> -->
        <template #header>
          <!-- 用一个行列布局 -->
          <el-row>
            <el-col :span="20">
              <span>{{ treeName.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="openAddDept()">添加顶级部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
        <!-- 页面的内容
        default-expand-all 默认展开所有节点
        -->
        <el-tree :data="treeData" :props="defaultProps" default-expand-all>
          <!--
            1. 默认插槽形式=》自定义每个节点的html结构
            2. 作用域插槽=》通过data获取节点数据
            注意：自定义结构只能有一个根元素
           -->
          <template #default="{ data }">
            <el-row style="width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager || "--" }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openAddDept(data,1)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="openAddDept(data,2)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delPart(data)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <AddDept ref="addRefs" :all-depts="allDepts" :show-dialog="showDialog" :parent-dept="parentDept" @close-dialog="showDialog=$event" @refresh-dept="getDpartments" />

    </div>
  </div>
</template>

<script>
import { getDepartmentsAPI, delDepartmentsAPI } from '@/api/departments'
import { tranfromTreeData } from '@/utils/index.js'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data () {
    return {
      allDepts: [],
      // 控制弹层显示与隐藏
      showDialog: false,
      // 公司部门数据（树形结构）
      treeData: [
      ],
      treeName: { name: '' },
      // 需求：后台返回的数据部门名叫name
      defaultProps: {
        children: 'children',
        label: 'name' // 指定渲染使用的部门属性名叫name
      },
      // 要添加子部门的父部门数据
      parentDept: null
    }
  },
  created () {
    this.getDpartments()
  },
  methods: {
    // 添加子部门控制弹出层
    openAddDept (parentDept, type) {
      this.parentDept = parentDept
      this.showDialog = true
      if (type === 2) {
        this.$refs.addRefs.getDeptDetil(parentDept.id)
      }
    },
    // 渲染数据
    async getDpartments () {
      const { depts, companyName } = await getDepartmentsAPI()
      // console.log('hahah', depts)
      this.allDepts = depts
      this.treeData = tranfromTreeData(depts)
      this.treeName.name = companyName
    },
    // 删除数据
    async delPart (curDept) {
      try {
        await this.$confirm(`确定删除${curDept.name}吗？`)
        if (curDept.children && curDept.children.length) return this.$message.error('不允许删除父节点')
        await delDepartmentsAPI(curDept.id)
        this.getDpartments()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 深度作用选择符：less 使用 /deep/   |  scss使用 ::v-deep
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
